from fastapi import APIRouter, Depends, Query
from starlette.responses import JSONResponse
from application import get_current_user
from application.logic import FollowLogic
from application.model.UserModel import UserModel
from application.util.ResponseUtil import ResponseUtil

# 设置路由
router: APIRouter = APIRouter(prefix="/follow", tags=["关注"])


@router.post("/add/{follow_id:int}", summary="关注用户")
async def follow_user(follow_id: int, user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await FollowLogic.follow_user(user_model=user_model, follower_id=follow_id)
    return ResponseUtil().success()


@router.post("/cancel/{follow_id:int}", summary="取消关注用户")
async def follow_cancel(follow_id: int, user_model: UserModel = Depends(get_current_user)) -> JSONResponse:
    await FollowLogic.cancel_follow(follower_id=follow_id, user_model=user_model)
    return ResponseUtil().success()


@router.get("/fans/{user_id:int}", summary="获取粉丝列表")
async def get_fans_by_user_id(user_id: int, page: int = Query(default=1, ge=1),
                              page_size: int = Query(default=10, ge=1)) -> JSONResponse:
    fans_list: list = await FollowLogic.get_fans_by_user_id(user_id=user_id, page_size=page_size, page=page)
    return ResponseUtil(data=fans_list).success()


@router.get("/follows/{user_id:int}", summary="获取关注列表")
async def get_fans_by_user_id(user_id: int, page: int = Query(default=1, ge=1),
                              page_size: int = Query(default=10, ge=1)) -> JSONResponse:
    follows_list: list = await FollowLogic.get_follows_by_user_id(user_id=user_id, page_size=page_size, page=page)
    return ResponseUtil(data=follows_list).success()
